package RPPM.servlet;

import RPPM.dao.impl.*;
import RPPM.domain.*;



import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/loginservlet/*")
public class LoginServlet extends BaseServlet {

    public void teaLogin(HttpServletRequest request, HttpServletResponse response)  throws IOException {
        request.setCharacterEncoding("utf-8");

        System.out.println("tealogin");

        String usernameInput = request.getParameter("username");
        String passwordInput = request.getParameter("password");

        Teacher toBechecked = new Teacher();
        toBechecked.setTea_id(Integer.parseInt(usernameInput));
        toBechecked.setTea_password(passwordInput);


        TeaDaoImpl teaService = new TeaDaoImpl();

        Teacher login = teaService.findTeacherById(toBechecked);


        ResultInfo info = new ResultInfo();

        response.setContentType("text/html;charset=utf-8");

        if (login != null) {
            // 用户存在，验证密码是否正确
            if (passwordInput.equals(login.getTea_password())) {
                // 密码正确，登录成功
                info.setFlag(true);
                info.setErrorMsg("欢迎回来，"+login.getTea_name()+"！");
                // 将登录用户存入session中
                request.getSession().setAttribute("teaid", login.getTea_id());
            } else {
                // 密码不正确
                info.setFlag(false);
                info.setErrorMsg("密码错误，请重新输入");
            }
        } else {
            // 用户不存在
            info.setFlag(false);
            info.setErrorMsg("ID不存在！");
        }

        writeJsonValue(response,info);
    }
    public void stuLogin(HttpServletRequest request, HttpServletResponse response)  throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("stulogin");

        String idInput = request.getParameter("username");
        String passwordInput = request.getParameter("password");



        Student toBechecked = new Student();
        toBechecked.setStu_id(Integer.parseInt(idInput));
        toBechecked.setStu_password(passwordInput);

        StuDaoImpl stuService = new StuDaoImpl();

        Student login = stuService.findStudentById(toBechecked);



        ResultInfo info = new ResultInfo();

        response.setContentType("text/html;charset=utf-8");

        if (login != null) {
            // 用户存在，验证密码是否正确
            if (passwordInput.equals(login.getStu_password())) {
                HttpSession stusession = request.getSession();
                stusession.setAttribute("stuid",login.getStu_id());
                // 密码正确，登录成功
                info.setFlag(true);
                info.setErrorMsg("欢迎回来，"+login.getStu_name()+"！");
            } else {
                // 密码不正确
                info.setFlag(false);
                info.setErrorMsg("密码错误，请重新输入");
            }
        } else {
            // 用户不存在
            info.setFlag(false);
            info.setErrorMsg("ID不存在！");
        }

        writeJsonValue(response,info);
    }
    public void exeLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("exelogin");

        String idInput = request.getParameter("username");
        String passwordInput = request.getParameter("password");



        Executor toBechecked = new Executor();
        toBechecked.setExe_id(Integer.parseInt(idInput));
        toBechecked.setExe_password(passwordInput);

        ExecutorDaoImpl executorDao = new ExecutorDaoImpl();

        Executor login = executorDao.findExecutorById(toBechecked);



        ResultInfo info = new ResultInfo();

        response.setContentType("text/html;charset=utf-8");

        if (login != null) {
            // 用户存在，验证密码是否正确
            if (passwordInput.equals(login.getExe_password())) {
                HttpSession stusession = request.getSession();
                stusession.setAttribute("exeid",login.getExe_id());
                // 密码正确，登录成功
                info.setFlag(true);
                info.setErrorMsg("欢迎回来，"+login.getExe_name()+"！");

            } else {
                // 密码不正确
                info.setFlag(false);
                info.setErrorMsg("密码错误，请重新输入");
            }
        } else {
            // 用户不存在
            info.setFlag(false);
            info.setErrorMsg("ID不存在！");
        }

        writeJsonValue(response,info);
    }
    public void mngLogin(HttpServletRequest request, HttpServletResponse response)  throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("mnglogin");

        String idInput = request.getParameter("username");
        String passwordInput = request.getParameter("password");
        System.out.println(idInput+ " " +passwordInput);



        Manager toBechecked = new Manager();
        toBechecked.setManager_id(Integer.parseInt(idInput));
        toBechecked.setManager_password(passwordInput);

        ManagerDaoImpl managerDao = new ManagerDaoImpl();
        System.out.println(toBechecked.getManager_id());

        Manager login = managerDao.findManagerById(toBechecked);

        ResultInfo info = new ResultInfo();

        response.setContentType("text/html;charset=utf-8");

        if (login != null) {
            // 用户存在，验证密码是否正确
            if (passwordInput.equals(login.getManager_password())) {
                HttpSession stusession = request.getSession();
                stusession.setAttribute("managerid",login.getManager_id());
                // 密码正确，登录成功
                info.setFlag(true);
                info.setErrorMsg("欢迎回来，"+login.getManager_name()+"！");
            } else {
                // 密码不正确
                info.setFlag(false);
                info.setErrorMsg("密码错误，请重新输入");
            }
        } else {
            // 用户不存在
            info.setFlag(false);
            info.setErrorMsg("ID不存在！");
        }

        writeJsonValue(response,info);
    }
    public void OnsiteStaffLogin(HttpServletRequest request, HttpServletResponse response)  throws IOException {
        request.setCharacterEncoding("utf-8");

        String idInput = request.getParameter("username");
        String passwordInput = request.getParameter("password");



        Onsitestaff toBechecked = new Onsitestaff();
        toBechecked.setStaff_id((Integer.parseInt(idInput)));
        toBechecked.setStaff_password(passwordInput);

        OnsitestaffDaoImpl onsitestaffDao = new OnsitestaffDaoImpl();

        Onsitestaff login = onsitestaffDao.findOnsitestaffById(toBechecked);



        ResultInfo info = new ResultInfo();

        response.setContentType("text/html;charset=utf-8");

        if (login != null) {
            // 用户存在，验证密码是否正确
            if (passwordInput.equals(login.getStaff_password())) {
                HttpSession stusession = request.getSession();
                stusession.setAttribute("staff_id",login.getStaff_id());
                // 密码正确，登录成功
                info.setFlag(true);
                info.setErrorMsg("欢迎回来，"+login.getStaff_name()+"！");

            } else {
                // 密码不正确
                info.setFlag(false);
                info.setErrorMsg("密码错误，请重新输入");
            }
        } else {
            // 用户不存在
            info.setFlag(false);
            info.setErrorMsg("ID不存在！");
        }

        writeJsonValue(response,info);
    }

}